package Limit.Game.MarkSix;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import l1j.server.L1DatabaseFactory;
import l1j.server.server.utils.SQLUtil;

public class MarkSix_RubbishTickets_Table {
	
	public void saveRubbishTickets(int char_id, int type, int wager, String[] ticketsNumInfo, Date dateTime)
	{
		Connection con = null;
		PreparedStatement pstm = null;
		try 
		{
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("INSERT INTO game_marksix_rubbishtickets SET char_id=?, Type=?, Wager=?, num1=?, num2=?, num3=?, num4=?, num5=?, num6=?, datetime =?");           
			
			int i=1;
			pstm.setInt(i++, char_id);
			pstm.setInt(i++, type);
			pstm.setInt(i++, wager);
			pstm.setString(i++, ticketsNumInfo[0]);
			pstm.setString(i++, ticketsNumInfo[1]);
			pstm.setString(i++, ticketsNumInfo[2]);
			pstm.setString(i++, ticketsNumInfo[3]);
			pstm.setString(i++, ticketsNumInfo[4]);
			pstm.setString(i++, ticketsNumInfo[5]);
			pstm.setDate(i++, dateTime);
			pstm.execute();

		}
		catch (Exception e)	{System.out.print(e.getMessage());}
		finally {SQLUtil.close(pstm);SQLUtil.close(con);}		
	}
	
	public String[] LoadTickets(int charid) 
	{
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		int i = 0;
		int j = TicketsSize(charid) * 2;
		int k = 0;
		String[] result = new String[j];
		
		for(i = 0; i < j; i++)
			result[i] = "";
		
		try 
		{
			i = 1;
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM game_marksix_rubbishtickets WHERE char_id=? Order By datetime");
			pstm.setInt(1, charid);
			rs = pstm.executeQuery();
			
			while (rs.next()) 
			{
				k = i + 1;
				int type = rs.getInt("Type");
				String typeName = "";
				String num1 = rs.getString("num1");
				String num2 = rs.getString("num2");
				String num3 = rs.getString("num3");
				String num4 = rs.getString("num4");
				String num5 = rs.getString("num5");
				String num6 = rs.getString("num6");
				switch (type)
				{
				case 1:
					typeName = "特碼";
					result[i] = "類型("+ typeName +")　注數("+rs.getInt("Wager")/10+")";
					result[k] = "號碼："+num1;
					break;
				case 2:
					typeName = "二星";
					result[i] = "類型("+ typeName +")　注數("+rs.getInt("Wager")/10+")";
					result[k] = "號碼："+num1+","+num2;
					break;
				case 3:
					typeName = "三星";
					result[i] = "類型("+ typeName +")　注數("+rs.getInt("Wager")/10+")";
					result[k] = "號碼："+num1+","+num2+","+num3;
					break;
				case 4:
					typeName = "四星";
					result[i] = "類型("+ typeName +")　注數("+rs.getInt("Wager")/10+")";
					result[k] = "號碼："+num1+","+num2+","+num3+","+num4;
					break;
				case 6:
					typeName = "香港";
					result[i] = "類型("+ typeName +")　注數("+rs.getInt("Wager")/10+")";
					result[k] = "號碼："+num1+","+num2+","+num3+","+num4+","+num5+","+num6;
					break;
				case 7:
					typeName = "小";
					result[i] = "類型("+ typeName +")　注數("+rs.getInt("Wager")/10+")";
					result[k] = "";
					break;
				case 8:
					typeName = "大";
					result[i] = "類型("+ typeName +")　注數("+rs.getInt("Wager")/10+")";
					result[k] = "";
					break;
				case 9:
					typeName = "單";
					result[i] = "類型("+ typeName +")　注數("+rs.getInt("Wager")/10+")";
					result[k] = "";
					break;
				case 10:
					typeName = "雙";
					result[i] = "類型("+ typeName +")　注數("+rs.getInt("Wager")/10+")";
					result[k] = "";
					break;
				}
				i = k;
				i++;
			}
		}
		catch (SQLException e)
		{ 
		}
		finally 
		{
			SQLUtil.close(rs, pstm, con);
		}
		return result;
	}
	
	public int TicketsSize(int charid)
	{
		int i = 0;
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try 
		{
			i=1;
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM game_marksix_rubbishtickets WHERE char_id=? Order By datetime");
			pstm.setInt(1, charid);
			rs = pstm.executeQuery();
			
			while (rs.next()) 
			{
				i++;
			}
		}
		catch (SQLException e) 
		{ 
		}
		finally
		{
			SQLUtil.close(rs, pstm, con);
		}
		return i;
	}
	
	public void deleteRubbishTickets()
	{
		Connection con = null;	
		PreparedStatement pstm = null;
		try 
		{			
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("DELETE FROM game_marksix_rubbishtickets");
			
			pstm.execute();
		} 
		catch (Exception e) {System.out.print(e.getMessage());} 
		finally {SQLUtil.close(pstm);SQLUtil.close(con);}
	}
}
